﻿using System;
using System.Diagnostics;

namespace AutoControlSpectrum.Extensions
{
    internal class ProcessHelper
    {
        /// <summary>
        /// 根据进程名称杀死进程
        /// </summary>
        /// <param name="processName">进程名称</param>
        public static bool KillProcessByName(string processName)
        {
            try
            {
                // 获取所有与指定名称匹配的进程
                Process[] processes = Process.GetProcessesByName(processName);
                foreach (Process process in processes)
                {
                    // 杀死进程
                    process.Kill();
                    process.WaitForExit();
                    Console.WriteLine($"进程 {processName} 已被杀死！");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"无法杀死进程 {processName}：{ex.Message}");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 打开指定路径的exe程序
        /// </summary>
        /// <param name="exePath">exe文件路径</param>
        public static bool StartProcess(string exePath)
        {
            try
            {
                // 启动exe程序
                Process.Start(exePath);
                Console.WriteLine($"程序 {exePath} 已启动！");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"无法启动程序 {exePath}：{ex.Message}");
            }
            return true;
        }
    }
}